using System;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using FarPoint.Excel;

namespace Spread.ExcelIO
{
    public partial class DocumentCaching : BaseSampleControl
    {
        public DocumentCaching()
        {
            InitializeComponent();
            Name = "Document Caching";
            InitializeControls();
            SetupSpreadSheet();
        }
        private void InitializeControls()
        {
            openComboBox.DroppedDown = false;
            saveComboBox.DroppedDown = false;
            foreach (object item in Enum.GetValues(typeof(ExcelSaveFlags)))
            {
                saveListBox.Items.Add(item);
            }
            foreach(object item in Enum.GetValues(typeof(ExcelOpenFlags)))
            {
                openListBox.Items.Add(item);
            }
        }
        private void PnlControls_MouseClick(object sender, MouseEventArgs e)
        {
            if (!saveListBox.Bounds.Contains(e.Location))
                saveListBox.Visible = false;
            if (!openListBox.Bounds.Contains(e.Location))
                openListBox.Visible = false;
        }

        private void SaveComboBox_MouseClick(object sender, MouseEventArgs e)
        {
            saveComboBox.DroppedDown = false;
            saveListBox.Visible = !saveListBox.Visible;
        }

        private void OpenComboBox_MouseClick(object sender, MouseEventArgs e)
        {
            openComboBox.DroppedDown = false;
            openListBox.Visible = !openListBox.Visible;
        }

        private void SetupSpreadSheet()
        {
            spread.OpenExcel("Template\\AnnualFinancialReport.xlsx", ExcelOpenFlags.NoFlagsSet);
        }

        private void FlowPanel_SizeChanged(object sender, EventArgs e)
        {
            foreach(Control control in flowPanel.Controls)
            {
                control.Width = flowPanel.Width - 10;
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            saveListBox.Visible = false;
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel 97-2003 Workboox(*.xls) | *.xls | Excel Workbook(*.xlsx) | *.xlsx";
            saveFileDialog.FilterIndex = -1;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = saveFileDialog.FileName;
                spread.SaveExcel(filename, GetSaveFlag() | ExcelSaveFlags.UseOOXMLFormat);
            }
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            openListBox.Visible = false;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel 97-2007 Workbook(*.xls)|*.xls|Excel Workbook(*.xlsx)|*.xlsx";
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog.FileName;
                spread.OpenExcel(fileName, GetOpenFlag());
            }
        }
        private ExcelSaveFlags GetSaveFlag()
        {
            ExcelSaveFlags result = ExcelSaveFlags.NoFlagsSet;
            foreach(var item in saveListBox.CheckedItems)
            {
                result |= (ExcelSaveFlags)item;
            }
            return result;
        }
        private ExcelOpenFlags GetOpenFlag()
        {
            ExcelOpenFlags result = ExcelOpenFlags.NoFlagsSet;
            foreach(var item in openListBox.CheckedItems)
            {
                result |= (ExcelOpenFlags)item;
            }
            return result;
        }
    }
}


//
//DocumentCaching.Designer.cs
//


namespace Spread.ExcelIO
{
    partial class DocumentCaching
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.openComboBox = new System.Windows.Forms.ComboBox();
            this.openListBox = new System.Windows.Forms.CheckedListBox();
            this.openButton = new System.Windows.Forms.Button();
            this.saveLabel = new System.Windows.Forms.Label();
            this.saveComboBox = new System.Windows.Forms.ComboBox();
            this.saveListBox = new System.Windows.Forms.CheckedListBox();
            this.saveButton = new System.Windows.Forms.Button();
            this.openLabel = new System.Windows.Forms.Label();
            this.flowPanel = new System.Windows.Forms.FlowLayoutPanel();
            this.spread = new FarPoint.Win.Spread.FpSpread();
            this.sheetView = new FarPoint.Win.Spread.SheetView();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            this.flowPanel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.spread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetView)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.spread);
            this.pnlMain.Size = new System.Drawing.Size(765, 487);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.flowPanel);
            this.pnlControls.Size = new System.Drawing.Size(253, 487);
            // 
            // openComboBox
            // 
            this.openComboBox.DropDownHeight = 1;
            this.openComboBox.FormattingEnabled = true;
            this.openComboBox.IntegralHeight = false;
            this.openComboBox.Location = new System.Drawing.Point(3, 16);
            this.openComboBox.Margin = new System.Windows.Forms.Padding(3, 3, 3, 0);
            this.openComboBox.MinimumSize = new System.Drawing.Size(10, 0);
            this.openComboBox.Name = "openComboBox";
            this.openComboBox.Size = new System.Drawing.Size(239, 21);
            this.openComboBox.TabIndex = 22;
            this.openComboBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.OpenComboBox_MouseClick);
            // 
            // openListBox
            // 
            this.openListBox.FormattingEnabled = true;
            this.openListBox.Location = new System.Drawing.Point(3, 37);
            this.openListBox.Margin = new System.Windows.Forms.Padding(3, 0, 3, 3);
            this.openListBox.Name = "openListBox";
            this.openListBox.Size = new System.Drawing.Size(239, 94);
            this.openListBox.TabIndex = 23;
            this.openListBox.Visible = false;
            // 
            // openButton
            // 
            this.openButton.Location = new System.Drawing.Point(3, 144);
            this.openButton.Margin = new System.Windows.Forms.Padding(3, 10, 3, 10);
            this.openButton.Name = "openButton";
            this.openButton.Size = new System.Drawing.Size(239, 23);
            this.openButton.TabIndex = 19;
            this.openButton.Text = "Open Excel";
            this.openButton.UseVisualStyleBackColor = true;
            this.openButton.Click += new System.EventHandler(this.openButton_Click);
            // 
            // saveLabel
            // 
            this.saveLabel.Location = new System.Drawing.Point(3, 187);
            this.saveLabel.Margin = new System.Windows.Forms.Padding(3, 10, 3, 0);
            this.saveLabel.Name = "saveLabel";
            this.saveLabel.Size = new System.Drawing.Size(239, 13);
            this.saveLabel.TabIndex = 20;
            this.saveLabel.Text = "Save Option:";
            // 
            // saveComboBox
            // 
            this.saveComboBox.DropDownHeight = 1;
            this.saveComboBox.FormattingEnabled = true;
            this.saveComboBox.IntegralHeight = false;
            this.saveComboBox.Location = new System.Drawing.Point(3, 203);
            this.saveComboBox.Margin = new System.Windows.Forms.Padding(3, 3, 3, 0);
            this.saveComboBox.Name = "saveComboBox";
            this.saveComboBox.Size = new System.Drawing.Size(239, 21);
            this.saveComboBox.TabIndex = 24;
            this.saveComboBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.SaveComboBox_MouseClick);
            // 
            // saveListBox
            // 
            this.saveListBox.FormattingEnabled = true;
            this.saveListBox.Location = new System.Drawing.Point(3, 224);
            this.saveListBox.Margin = new System.Windows.Forms.Padding(3, 0, 3, 3);
            this.saveListBox.Name = "saveListBox";
            this.saveListBox.Size = new System.Drawing.Size(239, 94);
            this.saveListBox.TabIndex = 25;
            this.saveListBox.Visible = false;
            // 
            // saveButton
            // 
            this.saveButton.Location = new System.Drawing.Point(3, 331);
            this.saveButton.Margin = new System.Windows.Forms.Padding(3, 10, 3, 3);
            this.saveButton.Name = "saveButton";
            this.saveButton.Size = new System.Drawing.Size(239, 23);
            this.saveButton.TabIndex = 21;
            this.saveButton.Text = "Save Excel";
            this.saveButton.UseVisualStyleBackColor = true;
            this.saveButton.Click += new System.EventHandler(this.saveButton_Click);
            // 
            // openLabel
            // 
            this.openLabel.Location = new System.Drawing.Point(3, 0);
            this.openLabel.Name = "openLabel";
            this.openLabel.Size = new System.Drawing.Size(239, 13);
            this.openLabel.TabIndex = 18;
            this.openLabel.Text = "Open Option:";
            // 
            // flowPanel
            // 
            this.flowPanel.Controls.Add(this.openLabel);
            this.flowPanel.Controls.Add(this.openComboBox);
            this.flowPanel.Controls.Add(this.openListBox);
            this.flowPanel.Controls.Add(this.openButton);
            this.flowPanel.Controls.Add(this.saveLabel);
            this.flowPanel.Controls.Add(this.saveComboBox);
            this.flowPanel.Controls.Add(this.saveListBox);
            this.flowPanel.Controls.Add(this.saveButton);
            this.flowPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.flowPanel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
            this.flowPanel.Location = new System.Drawing.Point(0, 0);
            this.flowPanel.Name = "flowPanel";
            this.flowPanel.Size = new System.Drawing.Size(253, 487);
            this.flowPanel.TabIndex = 0;
            this.flowPanel.SizeChanged += new System.EventHandler(this.FlowPanel_SizeChanged);
            this.flowPanel.MouseClick += new System.Windows.Forms.MouseEventHandler(this.PnlControls_MouseClick);
            // 
            // spread
            // 
            this.spread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.spread.Location = new System.Drawing.Point(0, 0);
            this.spread.Name = "spread";
            this.spread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.sheetView});
            this.spread.Size = new System.Drawing.Size(765, 487);
            this.spread.TabIndex = 0;
            // 
            // sheetView
            // 
            this.sheetView.Reset();
            this.sheetView.SheetName = "DocumentCaching";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.sheetView.RowHeader.Columns.Default.Width = 35F;
            this.sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // DocumentCaching
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "DocumentCaching";
            this.Size = new System.Drawing.Size(1019, 487);
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            this.flowPanel.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.spread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetView)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion
        private System.Windows.Forms.ComboBox openComboBox;
        private System.Windows.Forms.CheckedListBox openListBox;
        private System.Windows.Forms.Button openButton;
        private System.Windows.Forms.Label saveLabel;
        private System.Windows.Forms.ComboBox saveComboBox;
        private System.Windows.Forms.CheckedListBox saveListBox;
        private System.Windows.Forms.Button saveButton;
        private System.Windows.Forms.Label openLabel;
        private System.Windows.Forms.FlowLayoutPanel flowPanel;
        private FarPoint.Win.Spread.FpSpread spread;
        private FarPoint.Win.Spread.SheetView sheetView;
    }
}
